<?php
class Notepad_Settings {
	const SEPARATOR = '|';

    protected $file = null;
	public $settings = array();

	public function __construct() {
		//$this->file = PROJECT_DATA_DIR . 'settings.ini';
		//$this->settings = parse_ini_file($this->file, true);
		
		$this->settings = array(
			'strings.untitled' => 'untitled %d',
			'strings.modified' => '',
			'strings.MSGdelete' => 'Do you really want to delete the current file?',
			'strings.MSGreplace' => "<b>%d</b> replacements were made<br /><span font_desc='Sans Italic 8'><tab />'%s'</span><br />was replaced with:<br /><span font_desc='Sans Italic 8'><tab />'%s'</span><br />Do you accept the replacements?",

			'history.limit' => '30',
			'history.file' => 'history.dat',

			'texttags.tags' => 'highlight',

			'texttags_highlight.foreground' => 'black',
			'texttags_highlight.background' => 'yellow',
			'texttags_highlight.size-points' => '18'
		);
	}
	public function get($property) {
        list($section, $name) = explode('.', $property);
        if(isset($this->settings[$section][$name]))
        	return self::replaceTags($this->settings[$section][$name]);
        return false;
	}
	public function getSection($section) {
        if(isset($this->settings[$section]))
            return $this->settings[$section];
        return false;
	}
	public function getAll() {
		return $this->settings;
	}
	public function set($property, $val) {
        list($section, $name) = explode('.', $property);
        $this->settings[$section][$name] = $val;
        return true;
	}
	public function setSection($section, $data) {
		$this->settings[$section] = $data;
		return true;
	}
	public function setAll($data) {
		$this->settigns = $data;
	}
	public function save() {
		$content = '';
		foreach($this->settings as $sectionName => $section) {
			$content .= sprintf("[%s]\r\n", $sectionName);
			foreach($section as $propertyName => $property) {
                if(!(is_numeric($property) || is_bool($property)))
                	$property = sprintf('"%s"', $property);
				$content .= sprintf("%s = %s\r\n", $propertyName, $property);
			}
		}
		file_put_contents($this->file, trim($content));
		return true;
	}
    public static function replaceTags($str) {
    	$search = array(
    		'<br />',
    		'<tab />'
    	);
    	$replace = array(
    		"\r\n",
    		"\t"
    	);
    	return str_replace($search, $replace, $str);
    }
}
?>